package cn.com.nes.site.action.monitor;

import java.sql.SQLException;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.nes.site.entity.DataReq;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.monitor.DeviceService;

@Controller("device")
@RequestMapping("/device")
public class DeviceAction {

	@Resource
	private DeviceService service;

    /**
     * 项目设备分布图数量
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/getEquipmentCount")
    public @ResponseBody
    PageData getEquipmentCount(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getEquipmentCount(reqStatus.getReq());
        return res;
    }

	@RequestMapping(method = RequestMethod.POST, value = "/getEquipment")
    public @ResponseBody
    PageData getEquipment(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getEquipment(reqStatus.getReq());
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/getEquipmentImages")
    public @ResponseBody
    PageData getEquipmentImages(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getEquipmentImages(reqStatus.getReq());
        return res;
    }

	@RequestMapping(method = RequestMethod.POST, value = "/getStation")
    public @ResponseBody
    PageData getStation(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getStation(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/getRepairPeople")
    public @ResponseBody
    PageData getRepairPeople(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getRepairPeople(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/delEquipmentById")
    public @ResponseBody
    PageData delEquipmentById(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.delEquipmentById(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/getParamGroup")
    public @ResponseBody
    PageData getParamGroup(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getParamGroup(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/unbindEquipmentById")
    public @ResponseBody
    PageData unbindEquipmentById(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.unbindEquipmentById(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/getWorkOrder")
    public @ResponseBody
    PageData getWorkOrder(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getWorkOrder(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/getCardInfo")
    public @ResponseBody
    PageData getCardInfo(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getCardInfo(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/getRealFaultList")
    public @ResponseBody
    PageData getRealFaultList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getRealFaultList(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/getHisFaultList")
    public @ResponseBody
    PageData getHisFaultList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getHisFaultList(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/getRealWarns")
    public @ResponseBody
    PageData getRealWarns(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getRealWarns(reqStatus.getReq());
        return res;
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/getHisWarns")
    public @ResponseBody
    PageData getHisWarns(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getHisWarns(reqStatus.getReq());
        return res;
    }
	

	//获取该设备最新运行情况数据
	@RequestMapping(method = RequestMethod.POST, value = "/getEquipmentInfoOperationCondition")
    public @ResponseBody
    PageData getEquipmentInfoOperationCondition(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getEquipmentInfoOperationCondition(reqStatus.getReq());
        return res;
    }
	
	//修改名称
	@RequestMapping(method = RequestMethod.POST, value = "/addCustomField")
    public @ResponseBody
    PageData addCustomField(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.addCustomField(reqStatus.getReq());
        return res;
    }
	
	//实时数据-设备分组
	@RequestMapping(method = RequestMethod.POST, value = "/saveDataItemGroups")
    public @ResponseBody
    PageData saveDataItemGroups(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.saveDataItemGroups(reqStatus.getReq());
        return res;
    }
	
	//获取该设备历史运行情况数据
	@RequestMapping(method = RequestMethod.POST, value = "/getEquipmentInfoOperationConditionHistory")
    public @ResponseBody
    PageData getEquipmentInfoOperationConditionHistory(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getEquipmentInfoOperationConditionHistory(reqStatus.getReq());
        return res;
    }


    // 巡检设备
    @RequestMapping(method = RequestMethod.GET, value = "/checkDeviceWorkingState")
    public @ResponseBody PageData checkDeviceWorkingState(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.checkDeviceWorkingState(reqStatus.getReq());
        return res;
    }

    // 发送控制指令
    @RequestMapping(method = RequestMethod.POST, value = "/sendControlCommand")
    public @ResponseBody PageData sendControlCommand(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.sendControlCommand(reqStatus.getReq());
        return res;
    }

    // 查询控制指令日志
    @RequestMapping(method = RequestMethod.POST, value = "/getControlLog")
    public @ResponseBody PageData getControlLog(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getControlLog(reqStatus.getReq());
        return res;
    }

    // 编辑设备信息
    @RequestMapping(method = RequestMethod.POST, value = "/editEquipmentInfo")
    public @ResponseBody PageData editEquipmentInfo(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.editEquipmentInfo(reqStatus.getReq());
        return res;
    }
    
    /**
     * 更新设备详细信息
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SQLException
     */
    @RequestMapping(method = RequestMethod.POST, value = "/updateEquipment")
    public @ResponseBody
    PageData updateEquipment(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.updateEquipment(reqStatus.getReq());
        return res;
    }
    
    /**
     * 删除设备
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SQLException
     */
    @RequestMapping(method = RequestMethod.POST, value = "/deleteEquipment")
    public @ResponseBody
    PageData deleteEquipment(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.deleteEquipment(reqStatus.getReq());
        return res;
    }
    
    /**
     * 解绑
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SQLException
     */
    @RequestMapping(method = RequestMethod.POST, value = "/equipmentUntying")
    public @ResponseBody
    PageData equipmentUntying(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        String secret = null;
        DataReq req = reqStatus.getReq();
        secret = (String) req.getReqData().get("secret");
        int code = service.verifyUntyingSecret(secret,reqStatus.getReq());
        if (code != 0) {
            return PageData.error("请输入解绑密码");
        }
        res = service.equipmentUntying(req);
        return res;
    }


    @RequestMapping(method = RequestMethod.POST, value = "/unbindEquipment")
    public @ResponseBody
    PageData unbindEquipment(HttpServletRequest request,HttpServletResponse response) throws Exception  {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        String secret = null;
        secret = (String) reqStatus.getReq().getReqData().get("secret");
        int code = service.verifyUntyingSecret(secret,reqStatus.getReq());
        if (code != 0) {
            return PageData.error("解绑密码不正确");
        }
        res = service.unbindEquipment(reqStatus.getReq());
        return res;
    }
}
